using Godot;
using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using ZFramework;

namespace ZerAx.Roguelike
{
	public partial class Root : Node, IServer
	{
		[Export]
		private Game _game;
		public Game Game => _game;

		[Export]
		private GuiSystem _gui;
		public GuiSystem Gui => _gui;

		private Logic _logic;
		public Logic Logic => _logic;

		[Export]
		private PackedScene initialScene;

		private readonly Dictionary<int, string> playerNames = [];

		public override void _Ready()
		{
			Gui.LoadContent(initialScene);
		}

		public void SendChat(int id, string msg)
		{
		}

		public void Broadcast(IGameEvent e)
		{
			switch (e)
			{
				case JoinEvent join:
					GD.Print($"{join.playerName} joined the world.");
					playerNames.Add(join.id, join.playerName);
					break;
				
				case ChatEvent chat:
					if (playerNames.TryGetValue(chat.id, out var name))
						Gui.MakeToast($"{name}:{chat.message}");
					//GD.Print($"{playerNames[chat.playerId]}:{chat.message}");
					break;
			}
		}

		public void SendTo(int connId, IGameEvent e)
		{
			
		}

		private int count = 0;
		public void TestToast()
		{
			Gui.MakeToast($"Hello {count++}");
		}
	}
}
